<?php
/**
 * Created by PhpStorm.
 * User: guanfei
 * Date: 2018/4/8
 * Time: 17:00
 */

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UserBackend;

class HLloginController extends Controller
{
    /**
     *
     * @param $name 用户名
     * @param $password 密码
     * @return mixed
     */
    public function actionNameAndPassword($name, $password)
    {
        if ($name != null && $password != null) {
            return $this->CheckNameAndPassword($name, $password);
        }
    }

    /**
     * @param $name
     * @param $password
     * @return string
     */
    private function CheckNameAndPassword($name, $password)
    {
        $user = UserBackend::find()->where(['username' => $name])->one();
        if ($user != null) {
            $password_hash = UserBackend::find()->select('password_hash')->where(['username' => $name])->scalar();
            return $this->validatePassword($password, $password_hash);
        } else {
            return "用户名不存在";
        }
    }

    /**
     * @param $password
     * @param $password_hash
     * @return mixed
     */
    private function validatePassword($password, $password_hash)
    {
        if (Yii::$app->getSecurity()->validatePassword($password, $password_hash)) {
            return "密码正确";
        } else {
            return "密码错误";
        }
    }
}